package com.vj.analyzeLog;

import java.io.IOException;
import java.util.Iterator;
import java.util.List;

import junit.framework.Assert;

import org.apache.solr.client.solrj.SolrQuery;
import org.apache.solr.client.solrj.SolrServer;
import org.apache.solr.client.solrj.SolrServerException;
import org.apache.solr.client.solrj.impl.HttpSolrServer;
import org.apache.solr.client.solrj.response.QueryResponse;
import org.apache.solr.common.SolrDocument;
import org.apache.solr.common.SolrDocumentList;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;

public class SearchTest {

	private static final String HTTP_SOLR_URL = "http://192.168.71.145:8080/solr";// "http://localhost:8983/solr";
	private SolrServer server; 
	
	@Before
	public void setup(){
		System.out.println("----- build SolrServer --");
		server = new HttpSolrServer(HTTP_SOLR_URL);
	}
	
	@After
	public void release() throws SolrServerException, IOException{
		System.out.println("----- SolrServer release..\n\n");
		server.commit();
	}
	// by id
	@Test
	public void testQueryById() throws SolrServerException{
		System.out.println("------ testQueryById:");
		SolrQuery query = new SolrQuery();
		query.setQuery("id:*");
		query.setRows(20);
		
		QueryResponse res = server.query(query);
		SolrDocumentList docs = res.getResults();
		long start = docs.getStart();
		float maxScore = 0;//docs.getMaxScore();
		long numFound = docs.getNumFound();
		System.out.printf("start=%s, maxScre=%s, numFound=%d, %n", start, maxScore, numFound);
		
		Assert.assertNotNull(docs);
		Assert.assertTrue("no index found!!", numFound>0);
	}
	
	// by title
	@Test
	public void testQueryByTitle_full_title() throws SolrServerException {
		System.out.println("---@ testQueryByTitle_full_title:");
		String title = "GET mapreduce-nextgen yarnmrv2-resource-manager-nm-manager HTTP".toLowerCase();
		SolrQuery query = new SolrQuery();
		query.setQuery("log_title:"+title);
		query.setRows(22);
		
		QueryResponse resp = server.query(query);
		SolrDocumentList docs = resp.getResults();
		long start = docs.getStart();
		float maxScore = 0;//docs.getMaxScore();
		long numFound = docs.getNumFound();
		System.out.printf("start=%s, maxScre=%s, numFound=%d, %n", start, maxScore, numFound);
		
		Assert.assertNotNull(docs);
		Assert.assertTrue("no index found!!", numFound>0);
	}
	
	@Test
	public void testQueryByTitle_substring_title() throws SolrServerException {
		System.out.println("---@ testQueryByTitle_substring_title:");
		String title = "GET".toLowerCase();
		SolrQuery query = new SolrQuery();
		query.setQuery("log_title:"+title+"");
		query.setRows(22);
		
		QueryResponse resp = server.query(query);
		SolrDocumentList docs = resp.getResults();
		long start = docs.getStart();
		float maxScore = 0;//docs.getMaxScore();
		long numFound = docs.getNumFound();
		System.out.printf("start=%s, maxScre=%s, numFound=%d, %n", start, maxScore, numFound);
		
		Assert.assertNotNull(docs);

		Assert.assertTrue("no index found!!", numFound>0);
	}
	
	// by browser
	@Test
	public void testQueryByBrowser() throws SolrServerException {
		System.out.println("@@ testQueryByBrowser:");
		String browser = "compatible";
		SolrQuery query = new SolrQuery();
		query.setQuery("log_browser:" + browser);
		query.setRows(100);
		
		QueryResponse resp = server.query(query);
		SolrDocumentList docs = resp.getResults();
		long start = docs.getStart();
		float maxScore = 0;//docs.getMaxScore();
		long numFound = docs.getNumFound();
		System.out.printf("start=%s, maxScre=%s, numFound=%d, %n", start, maxScore, numFound);
		
		Assert.assertNotNull(docs);
		Assert.assertTrue(numFound > 0);		
	}
	
	// by content
	@Test
	public void testQueryByContent() throws SolrServerException{
		System.out.println("@@ queryByContent:");
		String contentKey = "Windows".toLowerCase();
		SolrQuery query = new SolrQuery("log_content:" + contentKey);
		query.setRows(100);
		
		QueryResponse resp = server.query(query);
		SolrDocumentList docs = resp.getResults();
		long start = docs.getStart();
		float maxScore = 0;//docs.getMaxScore();
		long numFound = docs.getNumFound();
		System.out.printf("start=%s, maxScre=%s, numFound=%d, %n", start, maxScore, numFound);
		
		Assert.assertNotNull(docs);
		Assert.assertTrue("NO INDEX FOUND!!", numFound > 0);		
	
		
	}
	
	// by all
	@Test
	public void testQueryByAll() throws SolrServerException{
		System.out.println("@@ queryByAll:");
		String key = "Mozilla".toLowerCase();
		SolrQuery query = new SolrQuery(key);
		query.setRows(200);
		
		QueryResponse resp = server.query(query);
		SolrDocumentList docs = resp.getResults();
		long start = docs.getStart();
		float maxScore = 0;//docs.getMaxScore();
		long numFound = docs.getNumFound();
		System.out.printf("start=%s, maxScre=%s, numFound=%d, %n", start, maxScore, numFound);
		
		Assert.assertNotNull(docs);
		Assert.assertTrue(numFound > 0);		
	
			
	}
	
	@Test
	public void testQueryWithOrder() throws SolrServerException{
		SolrQuery query = new SolrQuery();
		query.setQuery("*:*");
		query.addSortField("log_title", SolrQuery.ORDER.asc);
		QueryResponse resp = server.query(query);
		SolrDocumentList docs = resp.getResults();
		
		Assert.assertEquals(1L, docs.getNumFound());
	}
	
	@Test
	public void testAdvancedUsageWithFacetedQuery() throws SolrServerException{
		SolrQuery query = new SolrQuery().
							setQuery("Mozilla".toLowerCase())
							.setFacet(true)
							.setFacetMinCount(1)
							.setFacetLimit(8)
							.addFacetField("log_title")
							.addFacetField("log_content");
		QueryResponse rsp = server.query(query);
		
		Assert.assertEquals(1L, rsp.getResults().getNumFound());
		
	}
	
	@Test
	public void testHighlighting() throws SolrServerException{
		// <!-- field for the QueryParser to use when an explicit fieldname is absent
		//		 DEPRECATED: specify "df" in your request handler instead.
		//		 <defaultSearchField>text</defaultSearchField> -->
		SolrQuery query = new SolrQuery();
		
		//TODO to search all fields
		query.setQuery("Mozilla".toLowerCase()); // not work
		query.setQuery("log_content:mozilla");
		
		query.setHighlight(true)
			 .setHighlightSnippets(1);// mean what?
		query.setParam("hl.fl", "log_content");//, "log_title");
		
		QueryResponse rsp = server.query(query);
		
		SolrDocumentList docs = rsp.getResults();
		Iterator<SolrDocument> iter = docs.iterator();
		Assert.assertTrue(iter.hasNext());
		while(iter.hasNext()){
			SolrDocument doc = iter.next();
			String content = (String) doc.getFieldValue("log_content");
			String id = (String) doc.getFieldValue("id");
			
			if(rsp.getHighlighting().get(id) != null) {
//				Assert.assertTrue(false); // go to here
				List<String> highlightSnippets = rsp.getHighlighting().get(id).get("log_content"); // [GET mapreduce-nextgen yarnmrv2-resource-manager-nm-manager HTTP <em>Mozilla</em> compatible MSIE Windows NT]
				Assert.assertTrue(highlightSnippets.size() > 0);
			}
		}
		
		Assert.assertEquals(1L, rsp.getResults().getNumFound());
	}
}
